package com.linkage.usercenter.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.exception.ServiceException;
import com.linkage.usercenter.dao.BannerMapper;
import com.linkage.usercenter.pojo.entity.Banner;
import com.linkage.usercenter.pojo.vo.BannerVo;
import com.linkage.usercenter.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * banner表 服务实现类
 * </p>
 *
 * @author shibiao
 * @since 2020-06-08
 */
@Service("bannerService")
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public List<Banner> getList() {
        Banner banner = new Banner();
        banner.setStatus(1);
        return  bannerMapper.queryBannerList(banner);
    }

    @Override
    public boolean insertBanner(Banner banner) {
        banner.setId(UUID.fastUUID().toString(true));
        bannerMapper.insertBannerInfo(banner);
        return true;
    }

    @Override
    public Banner getBannerInfo(String id) {
        return bannerMapper.queryBannerInfo(id);
    }

    @Override
    public boolean updateBanner(Banner banner) {
        if (StrUtil.isEmpty(banner.getId())){
            throw new ServiceException(1,"id参数为空");
        }
        bannerMapper.updateBanner(banner);
        return true;
    }

    @Override
    public PageInfo<Banner> getBannerListByPage(RequestInfo<BannerVo> requestInfo) {
        Integer page = StrUtil.isEmpty(requestInfo.getBase().getPageNum())?0:Integer.parseInt(requestInfo.getBase().getPageNum());
        Integer pageSize = StrUtil.isEmpty(requestInfo.getBase().getPageRows())?10:Integer.parseInt(requestInfo.getBase().getPageRows());
        PageHelper.startPage(page,pageSize);
        Banner banner = new Banner();
        banner.setStatus(requestInfo.getBuss().getStatus());
        List<Banner> bannerList = bannerMapper.queryBannerList(banner);
        PageInfo<Banner> pageInfo = new PageInfo<>(bannerList);
        return pageInfo;
    }

    @Override
    public void deleteBanner(String id){
        bannerMapper.deleteBanner(id);
    }
}
